﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class IndexAdImgController : Controller
    {
        private const string INDEXADIMG_PATH = "IndexADImgs/";

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllImgByPage(StoreRequestParameters parms)
        {
            WI_IndexAdImgDAL gdal = new WI_IndexAdImgDAL();
            int ttlCount;
            List<WI_IndexADImg> wimglist = gdal.GetAllADImages(parms.Start, parms.Limit, out ttlCount);
            List<IndexADImg> imgList = new List<IndexADImg>();
            foreach (var wimg in wimglist)
            {
                IndexADImg img = IndexADImg.AsIndexAdImg(wimg);
                imgList.Add(img);
            }
            return this.Store(imgList, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteImg(List<IndexADImg> delImgs)
        {
            try
            {
                WI_IndexAdImgDAL gdal = new WI_IndexAdImgDAL();
                foreach (var img in delImgs)
                {
                    gdal.RemoveADImg(img.ImgID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewImgWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建图片",
                Width = 500,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateImg"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateImg()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateImg(IndexADImg vimg)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                WI_IndexAdImgDAL gdal = new WI_IndexAdImgDAL();
                int imgId = gdal.CreateADImg("", vimg.SortNum, vimg.ImgName);
                //上传图片
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("ADImg");
                    string savePath = string.Empty;
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + INDEXADIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += "adimg_" + imgId + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            string webpath = uploadVFolder + INDEXADIMG_PATH + "adimg_" + imgId + extName;
                            gdal.UpdateADImg(imgId, webpath, vimg.SortNum, vimg.ImgName);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "图片创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditImgWindow(int imgId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑图片",
                Width = 500,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditImg"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("imgId", imgId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditImg(int imgId)
        {
            WI_IndexAdImgDAL gdal = new WI_IndexAdImgDAL();
            WI_IndexADImg wimg = gdal.GetADImg(imgId);
            IndexADImg img = IndexADImg.AsIndexAdImg(wimg);
            return View(img);
        }

        [HttpPost]
        public ActionResult EditImg(IndexADImg vimg)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                WI_IndexAdImgDAL gdal = new WI_IndexAdImgDAL();
                gdal.UpdateADImg(vimg.ImgID, vimg.ImgUrl, vimg.SortNum, vimg.ImgName);
                int imgId = vimg.ImgID;
                //上传图片
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("ADImg");
                    string savePath = string.Empty;
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + INDEXADIMG_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += "adimg_" + imgId + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            string webpath = uploadVFolder + INDEXADIMG_PATH + "htimg_" + imgId + extName;
                            gdal.UpdateADImg(imgId, webpath, vimg.SortNum, vimg.ImgName);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "图片更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
